package example;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;

public class CustomView extends View implements OnTouchListener{
	
	int layoutWidth = 0;
	int layoutHeight = 0;
	
	Paint mPaint;

	public CustomView(Context context) {
		super(context);
		init();
	}

	public CustomView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}

	public CustomView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}
	
	public void init(){
		mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		mPaint.setColor(Color.BLUE);
		setOnTouchListener(this);
	}
	
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		
		layoutWidth = MeasureSpec.getSize(widthMeasureSpec);
		layoutHeight = MeasureSpec.getSize(heightMeasureSpec);
		
		int modeWidth = MeasureSpec.getMode(widthMeasureSpec);
		int modeHeight = MeasureSpec.getMode(heightMeasureSpec);
		
		if(modeWidth==MeasureSpec.AT_MOST){
			Log.d("TEST", "AT_MOST");  //wrap_content
		}else if(modeWidth==MeasureSpec.EXACTLY){
			Log.d("TEST", "EXACTLY"); //fill_parent, 70dp
		}else if(modeWidth==MeasureSpec.UNSPECIFIED){
			Log.d("TEST", "UNSPECIFIED");
		}else{
			Log.d("TEST", "UNKNOW");
		}
	
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		
		int a = Math.min(layoutWidth, layoutHeight);
		
		canvas.drawCircle(layoutWidth/2, layoutHeight/2,a/2, mPaint);
//		setMeasuredDimension(a, a);
	}
	
	@SuppressLint("NewApi")
	@Override
	public boolean onTouch(View v, MotionEvent event) {
//		float x = v.getX();
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			Log.d("TEST", "down ");
			break;
		case MotionEvent.ACTION_MOVE:
			Log.d("TEST", "move ");
			break;
		case MotionEvent.ACTION_UP:
			Log.d("TEST", "up ");
			break;

		default:
			break;
		}
		
		//fale:chi nhan 1 su kien (down)
		//true: down, move, up
		return true;
	}

}
